#include "userinfowidget.h"
#include "mainwidget.h"
#include "model/datacenter.h"

#include <QGridLayout>
#include <QLabel>
#include <QMessageBox>
#include <QPushButton>

UserInfoWidget::UserInfoWidget(QWidget* parent, const UserInfo& userInfo)
    : QDialog(parent), userInfo(userInfo)
{
    // 1.设置窗口基本属性
    this->setStyleSheet("QWidget { background-color: rgb(253, 253, 253); }");
    this->setFixedSize(300, 150);
    this->setWindowTitle("用户信息");
    this->setWindowIcon(QIcon(":/resource/image/logo.png"));
    this->setAttribute(Qt::WA_DeleteOnClose);
    this->move(QCursor::pos());

    // 2. 整个窗口采用垂直布局(上边为头像 + 个人信息, 下边为功能按钮)
    QVBoxLayout* mainLayout = new QVBoxLayout();
    mainLayout->setSpacing(0);
    mainLayout->setContentsMargins(0, 0, 0, 0);
    this->setLayout(mainLayout);

    // 2-1 上半部分网格布局个人信息
    QGridLayout* infoLayout = new QGridLayout();
    infoLayout->setContentsMargins(10, 10, 0, 0);
    infoLayout->setHorizontalSpacing(5);
    infoLayout->setAlignment(Qt::AlignTop);
    mainLayout->addLayout(infoLayout);

    avatarBtn = new QPushButton();
    avatarBtn->setFixedSize(75, 75);
    avatarBtn->setIconSize(QSize(75, 75));
    avatarBtn->setIcon(userInfo.avatar);
    avatarBtn->setStyleSheet("QPushButton { border: none; background-color: white; }");

    idInfo = new InfoLine("序号:", userInfo.userId);
    nameInfo = new InfoLine("昵称:", userInfo.nickname);
    phoneInfo = new InfoLine("电话:", userInfo.phone);

    infoLayout->addWidget(avatarBtn, 0, 0, 3, 1);
    infoLayout->addWidget(idInfo->key, 0, 1);
    infoLayout->addWidget(idInfo->val, 0, 2);
    infoLayout->addWidget(nameInfo->key, 1, 1);
    infoLayout->addWidget(nameInfo->val, 1, 2);
    infoLayout->addWidget(phoneInfo->key, 2, 1);
    infoLayout->addWidget(phoneInfo->val, 2, 2);

    // 2-2 下半部分水平布局功能按钮
    QHBoxLayout* btnLayout = new QHBoxLayout();
    btnLayout->setSpacing(0);
    btnLayout->setContentsMargins(0, 0, 0, 20);
    mainLayout->addLayout(btnLayout);

    applyBtn = createButton("申请好友");
    sendMessageBtn = createButton("发送消息");
    deleteFriendBtn = createButton("删除好友");

    btnLayout->addWidget(applyBtn);
    btnLayout->addWidget(sendMessageBtn);
    btnLayout->addWidget(deleteFriendBtn);

    // 3. 按钮权限设置
    model::DataCenter* dataCenter = model::DataCenter::getInstance();
    model::UserInfo* myFriend = dataCenter->getFriendByUserId(this->userInfo.userId);
    if(myFriend) {
        applyBtn->setEnabled(false);
    } else {
        sendMessageBtn->setEnabled(false);
        deleteFriendBtn->setEnabled(false);
    }

    initSignalSlot();
}

void UserInfoWidget::initSignalSlot()
{
    connect(sendMessageBtn, &QPushButton::clicked, this, [=](){
        MainWidget* mainWidget = MainWidget::getInstance();
        mainWidget->switchToSession(this->userInfo.userId);
        this->close();
    });

    connect(deleteFriendBtn, &QPushButton::clicked, this, &UserInfoWidget::onClickedByDeleteFriendBtn);

    connect(applyBtn, &QPushButton::clicked, this, &UserInfoWidget::onClickedByApplyBtn);
}

void UserInfoWidget::onClickedByDeleteFriendBtn()
{
    QMessageBox::StandardButton ret = QMessageBox::warning(this, "确认删除", "是否确认删除当前好友", QMessageBox::Ok | QMessageBox::Cancel);
    if(ret != QMessageBox::Ok) {
        LOG(LL_INFO, "取消删除好友");
        return;
    }

    // 请求的响应处理函数是在主窗口中去操作的, 因此响应函数的信号槽关联置于主窗口内去完成
    model::DataCenter* dataCenter = model::DataCenter::getInstance();
    dataCenter->deleteFriendAsync(this->userInfo.userId);

    this->close();
}

void UserInfoWidget::onClickedByApplyBtn()
{
    model::DataCenter* dataCenter = model::DataCenter::getInstance();
    dataCenter->addFriendApplyAsync(this->userInfo.userId);
}

QPushButton* UserInfoWidget::createButton(const QString &text)
{
    static QString btnStyle = "QPushButton { border: 1px solid rgb(90, 90, 90); border-radius: 5px; } "
                              "QPushButton:pressed { background-color: rgb(235, 235, 235); }";
    QPushButton* button = new QPushButton();
    button->setStyleSheet(btnStyle);
    button->setFixedSize(80, 30);
    button->setText(text);
    return button;
}

UserInfoWidget::InfoLine::InfoLine(const QString &keyString, const QString& valString)
{
    static QString keyStyle = "QLabel { color: rgb(150, 150, 150); font-weight: 600; padding-left: 5px; }";
    static QString valStyle = "QLabel { color: rgb(150, 150, 150); font-weight: 600; }";
    static QSize keySize(35, 20);
    static QSize valSize(80, 20);

    key = new QLabel();
    key->setText(keyString);
    key->setFixedSize(keySize);
    key->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
    key->setAlignment(Qt::AlignCenter);
    key->setStyleSheet(keyStyle);

    val = new QLabel();
    val->setText(valString);
    val->setFixedSize(valSize);
    val->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
    val->setAlignment(Qt::AlignLeft | Qt::AlignVCenter);
    val->setStyleSheet(valStyle);

    key->setFont(QFont("SimSun", 9));
    val->setFont(QFont("SimSun", 9));
}
